import { Article, } from '@/entity/Article';
import { Categories } from '@/entity/Categories';
import { Tag } from '@/entity/Tag';
import { Comment } from '@/entity/Comment';
import { User } from '@/entity/User';
import { Service, Inject } from 'typedi'
import { Like } from "typeorm"
import { Entity } from '@/utils/orm'
import { Segment } from '@/utils/segment'
import { JsonResult, msgList, res } from '@/utils/jsonResult';
import { Article as ResArticle, comment as commentType, categories, Articlelabel, tag } from '@/m_res/Article';


@Service()
export class ArticleDao {

    @Inject() 
    orm!: Entity

    @Inject()
    json!: JsonResult<any>;

    @Inject()
    segment!: Segment

    //全部标签
    async GetArticlelabelDao(): Promise<res<Array<tag | null>>> {
        return this.json.success(await this.orm.get(Tag).find())
    }

    //标签 详情
    async GetArticlelabeldetailsDao(id: number): Promise<res<Article[]>> {
        console.log(111)
        let res= await this.orm.get(Article).findBy({
            tag_id: Like(`%${id}%`),
        })
        res.forEach(v => {
            v.article_text = v.article_text.substr(0, 100)
        })
        return this.json.success(res)
    }

    // 分类 全部
    async GetArticgoriesListDao(): Promise<res<Categories[]>> {
        return this.json.success(await this.orm.get(Categories).find())
    }

    // 分类 详情 
    async GetArticgoriesListinfoDao(id: number): Promise<res<Article[]>> {
       let ret=  await this.orm.get(Article).findBy({
            categories_id: Like(`%${id}%`),
        })
        ret.forEach(v => {
            v.article_text = v.article_text.substr(0, 100)
        })
        return this.json.success(ret)
    }

    //搜索  

    async GetSearchDao(text:string): Promise<res<Article[]>> {
        let sql: string = ""
        this.segment.ik(text).forEach(v => {
            sql += ` or article_title like '%${v.w}%'`
        })

        let res = await this.orm.repository(Article)
        .where(sql.replace("or", ""))
        .getMany()
        return this.json.success(res)
    }
}